﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OpenTKLightingTool.Data;

namespace OpenTKLightingTool.Controls
{
    /// <summary>
    /// Interaction logic for MaterialList.xaml
    /// </summary>
    public partial class MaterialList : UserControl
    {
        private MaterialDictionary mData;
        private int mPreviousIndex = -1;

        public MaterialList()
        {
            InitializeComponent();

            uxMaterials.SelectionUnit = DataGridSelectionUnit.FullRow;
            uxMaterials.SelectionChanged += uxMaterials_SelectionChanged;
        }

        private void uxMaterials_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (mPreviousIndex >= 0)
            {
                mData[uxMaterials.Items[mPreviousIndex].ToString()] = uxProperties.GetMaterial();
            }
            if (uxMaterials.SelectedIndex >= 0)
            {
                uxProperties.SetMaterial(mData[uxMaterials.SelectedValue.ToString()]);
            }
            mPreviousIndex = uxMaterials.SelectedIndex;
        }

        internal void SetData(MaterialDictionary data)
        {
            mData = data;
            uxMaterials.ItemsSource = data.Select(i => i.Key).ToList();
            if (uxMaterials.Items.Count > 0)
                uxMaterials.SelectedIndex = 0;
        }

        internal void Update()
        {
            if (mPreviousIndex >= 0)
            {
                mData[uxMaterials.Items[mPreviousIndex].ToString()] = uxProperties.GetMaterial();
            }
        }
    }
}
